package com.cxm.qyyz.base.fragment;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cxm.qyyz.R;
import com.cxm.qyyz.base.mvp.BaseContract;
import com.cxm.qyyz.core.http.DefaultObserver;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.trello.rxlifecycle4.LifecycleTransformer;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import per.goweii.anylayer.AnyLayer;
import per.goweii.anylayer.dialog.DialogLayer;

public abstract class BaseListFragment<M, T extends BaseContract.BasePresenter> extends BaseLazyLoadFragment<T>
        implements OnRefreshLoadMoreListener {

    //列表
    public RecyclerView list;
    public SmartRefreshLayout refreshLayout;
    LinearLayout empty;
    ImageView emptyImage;
    TextView emptyText;
    TextView emptyEnter;
    public BaseQuickAdapter listAdapter;


    //空文案点击事件
    public void onEmptyClick() {

    }

    //下一步文案  空等于不显示
    protected String getNextText() {
        return "";
    }

    //空文案
    protected abstract int getEmptyText();

    //空图片
    protected abstract int getEmptyImage();

    protected RecyclerView.LayoutManager getLayoutLayoutManager() {

        return new LinearLayoutManager(getActivity());
    }

    protected abstract BaseQuickAdapter getListAdapter();

    public boolean isAdd = true;

    public boolean isAdd() {
        return isAdd;
    }

    public DefaultObserver<ArrayList<M>> getCallBack() {
        return new DefaultObserver<ArrayList<M>>(this) {

            @Override
            protected void onSuccess(ArrayList<M> data) {
                if (null==list)return;
                getActivity().runOnUiThread(() -> {
                    if (null != data) {
                        if (data.size() > 0) {
                            if (pagerNumber == 1) {
                                listAdapter.setNewInstance(data);
                                refreshLayout.setEnableLoadMore(true);
                            } else {
                                listAdapter.addData(data);
                            }
                            if (isAdd())
                                pagerNumber++;
                            empty.setVisibility(View.GONE);
                            list.setVisibility(View.VISIBLE);
                            setEnableLoadMore(true);
                        } else {
                            if (pagerNumber == 1) {
                                listAdapter.setNewData(new ArrayList());
                            }
                            if (listAdapter.getData().size() == 0) {
                                empty.setVisibility(View.VISIBLE);
                                list.setVisibility(View.GONE);
                            }
                            setEnableLoadMore(false);
                        }
                    } else {
                        if (pagerNumber == 1) {
                            listAdapter.setNewInstance(new ArrayList());
                        }
                        if (listAdapter.getData().size() == 0) {
                            empty.setVisibility(View.VISIBLE);
                            list.setVisibility(View.GONE);
                        }
                        setEnableLoadMore(false);
                    }
                    closeLoding();
                });

            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                if (listAdapter.getData().size() == 0) {
                    empty.setVisibility(View.VISIBLE);
                    list.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.GONE);
                    list.setVisibility(View.VISIBLE);
                }
                closeLoding();
            }

        };
    }

    @Override
    public void onDismiss() {
        super.onDismiss();
        closeLoding();
    }

    public void setEnableLoadMore(boolean isrefresh) {
        refreshLayout.setEnableLoadMore(isrefresh);
    }

    public void setEnableRefresh(boolean isrefresh) {
        refreshLayout.setEnableRefresh(isrefresh);
    }

    public void autoRefresh() {
        if (null != refreshLayout)
            refreshLayout.autoRefresh();
    }

    public void closeLoding() {
        if (null != refreshLayout){
            refreshLayout.finishLoadMore();
            refreshLayout.finishRefresh();
        }
    }

    public int pagerNumber = 1;


    protected abstract void initUrl();


//    protected abstract int getLayoutId();


    @Override
    public void initView(View view) {
        super.initView(view);
        list = view.findViewById(R.id.list);
        refreshLayout = view.findViewById(R.id.refreshLayout);
        empty = view.findViewById(R.id.empty);
        emptyImage = view.findViewById(R.id.empty_image);
        emptyText = view.findViewById(R.id.empty_text);
        emptyEnter = view.findViewById(R.id.empty_enter);

        emptyImage.setImageDrawable(getResources().getDrawable(getEmptyImage()));
        emptyText.setText(getResources().getString(getEmptyText()));
        emptyEnter.setText(getNextText());
        emptyEnter.setVisibility(TextUtils.isEmpty(getNextText()) ? View.GONE : View.VISIBLE);
        emptyEnter.setOnClickListener(v -> onEmptyClick());

        refreshLayout.setOnRefreshLoadMoreListener(this);
        list.setLayoutManager(getLayoutLayoutManager());
        listAdapter = getListAdapter();
        list.setAdapter(listAdapter);
    }



    @Override
    public void onReload() {
        initUrl();
    }

    @Override
    public void jumpToLogin() {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        initUrl();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pagerNumber = 1;
        initUrl();
    }

    DialogLayer dialogLayer;

    /**
     * dialog
     * date 2021/12/20
     * author yzx
     */
    public void showDialog() {
//        if (null == dialogLayer)
//            dialogLayer = AnyLayer.dialog(getActivity())
//                    .contentView(R.layout.item_progressbar)
//                    .gravity(Gravity.CENTER)
//                    .cancelableOnTouchOutside(false)
//                    .cancelableOnClickKeyBack(false);
//
//        dialogLayer.show();
    }

    /**
     * 取消弹框
     * date 2021/12/20
     * author yzx
     */
    public void dismiss() {
        if (null != dialogLayer) dialogLayer.dismiss();
    }
}
